package cn.fintecher.pangolin.service.business.web;

import cn.fintecher.pangolin.common.web.BaseController;
import cn.fintecher.pangolin.entity.elastic.Warning;
import cn.fintecher.pangolin.service.business.model.request.WarningRequest;
import cn.fintecher.pangolin.service.business.model.response.FindWarningResponse;
import cn.fintecher.pangolin.service.business.repository.elasticsearch.WarningElastic;
import cn.fintecher.pangolin.service.business.service.WarningService;
import io.swagger.annotations.*;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Type;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: xiaqun
 * @Description: 警告相关接口
 * @Date: 15:15 2019/10/12
 */

@RestController
@RequestMapping("/api/warningController")
@Api(value = "警告相关接口", description = "警告相关接口")
public class WarningController extends BaseController {

    @Autowired
    private WarningService warningService;

    @Autowired
    private WarningElastic warningElastic;

    @Autowired
    private ModelMapper modelMapper;

    /**
     * 添加警告
     */
    @ApiOperation(value = "添加警告", notes = "添加警告")
    @PostMapping("/addWarning")
    public ResponseEntity<Void> addWarning(@RequestBody WarningRequest request,
                                           @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to add warning,{}", request);
        warningService.addWarning(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改警告
     */
    @ApiOperation(value = "修改警告", notes = "修改警告")
    @PutMapping("/modifyWarning")
    public ResponseEntity<Void> modifyWarning(@RequestBody WarningRequest request,
                                              @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to modify warning,{}", request);
        warningService.modifyWarning(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除警告
     */
    @ApiOperation(value = "删除警告", notes = "删除警告")
    @DeleteMapping("/deleteWarning")
    public ResponseEntity<Void> deleteWarning(@RequestParam @ApiParam(value = "警告ID", required = true) String id) {
        log.info("REST request to delete warning,{}", id);
        warningService.deleteWarning(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 查询警告
     */
    @ApiOperation(value = "查询警告", notes = "查询警告")
    @GetMapping("/findAllWarning")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<FindWarningResponse>> findAllWarning(@RequestParam @ApiParam(value = "案件ID", required = true) String caseId,
                                                                    Pageable pageable) {
        log.info("REST request to find all warning,{}", caseId);
        Page<FindWarningResponse> page = warningService.findAllWarning(caseId, pageable);
        return ResponseEntity.ok().body(page);
    }


    @ApiOperation(value = "根据案件id查询警告", notes = "根据案件id查询警告")
    @GetMapping("/getWarningByCaseId")
    public ResponseEntity<List<String>> getWarningByCaseId(@RequestParam @ApiParam(value = "案件ID", required = true) String caseId) {
        List<Warning> warningList = warningElastic.findByCaseId(caseId);
        List<String> stringList = warningList.stream().filter(e -> e.getFlag() == 0).map(Warning::getContent).collect(Collectors.toList());
        return ResponseEntity.ok().body(stringList);
    }
}
